﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

namespace webs.website_admin.integration
{
    public partial class IntegralHistoryEditor : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            PageBegin();


            /// 保存设置
            if (RequestBase.GetFormString("___EVENTTARGET") == "save")
            {
                Save(RequestBase.GetFormString("___EVENTARGUMENT"));
            }

            if (!IsPostBack)
            {

                if (RequestBase.GetQueryString("id") != "")
                    Initialize();

            }
        }



        #region 数据初始化
        void Initialize()
        {
            Model.IntegralHistoryBase _obj = BLL.IntegralBase.GetIntegralObjById(RequestBase.GetQueryString("id"));
            Integral = _obj;

        }

        Model.IntegralHistoryBase Integral
        {
            get
            {
                Model.IntegralHistoryBase integral = new Model.IntegralHistoryBase();

                if (!string.IsNullOrEmpty(RequestBase.GetQueryString("id")))
                    integral.Id = Convert.ToInt32(RequestBase.GetQueryString("id"));

                integral.UserId = txtUserName.Value.Trim();
                integral.ComesFrom = txtDescripiton.Text.Trim();
                integral.IntegralCount = Convert.ToInt32(txtCount.Value);
                integral.CreatedDate = Convert.ToDateTime(txtCDate.Value);
                integral.ExpirationDate = Convert.ToDateTime(txtEDate.Value);
                integral.IsValid =  chkIsValid.Checked;
                integral.ValidIntegralCount = Convert.ToInt32(txtValidCount.Value);
                return integral;
            }
            set
            {
                if (value != null)
                {
                    lblUserName.InnerText = value.UserName;

                    txtUserName.Value = value.UserId;
                    txtDescripiton.Text = value.ComesFrom;
                    txtCount.Value = value.IntegralCount.ToString();
                    txtEDate.Value = value.ExpirationDate.ToShortDateString();
                    txtCDate.Value = value.CreatedDate.ToShortDateString();
                    txtValidCount.Value = value.ValidIntegralCount.ToString();
                    chkIsValid.Checked = value.IsValid;

                }
            }
        }

        #endregion

        #region 保存数据
        void Save(string omode)
        {
            string res = "";

            if (RequestBase.GetQueryString("otype").Trim() == "add")
            {
                res = BLL.IntegralBase.CreateIntegral(Integral);
            }

            if (RequestBase.GetQueryString("otype").Trim() == "modify")
            {
                 res = BLL.IntegralBase.ModifyIntegral(Integral);
            }

            switch (res.Split('|')[0])
            {
                case "succeeded":
                    if (omode == "saveandclose")
                        JScript.AlertAndReturnValueToParent("信息保存成功!", "window.opener.LoadingData();window.close();");
                    else
                        JScript.AlertAndReturnValueToParent("信息保存成功!", "window.opener.LoadingData();window.location.href=window.location.href;");
                    break;
                default:
                    JScript.Alert(res);
                    break;
            }
        }
        #endregion
    }
}